layui.define([ 'form', 'laydate', 'table' ], function(exports) {
    var form = layui.form;
    var laydate = layui.laydate;
    var table = layui.table;
    var coreDeviceTable = null;
    var view ={
        init:function(){
            this.initTable();
            this.initSearchForm();
            this.initToolBar();
            window.dataReload = function(){
                Lib.doSearchForm($("#searchForm"),coreDeviceTable);
                Lib.cleanSearchForm($("#searchForm"),coreDeviceTable);
            }
        },
        initTable:function(){
            coreDeviceTable = table.render({
                elem : '#coreDeviceTable',
                height : Lib.getTableHeight(1),
                cellMinWidth: 100,
                method : 'post',
                url : Common.ctxPath + '/cms/coreDevice/list.json' // 数据接口
                ,page: true// 开启分页
                ,limit : 10,
                cols : [ [ // 表头
                    {
                        type : 'checkbox',
                        fixed:'left',
                    },
                {

                    field : 'id', 
                        title : 'id',
                    fixed:'left',
                        width : 60,
                },
                {

                    field : 'dname', 
                        title : '设备名称',
                },
                {

                    field : 'unitType', 
                        title : '设备型号',
                },
                {

                    field : 'code', 
                        title : '设备号',
                },
                {

                    field : 'img', 
                        title : '设备证书',
                },
                {

                    field : 'createTime', 
                        title : '创建时间',
                },
                {

                    field : 'comName',
                        title : '所属企业',
                },
                {

                    field : 'deadline', 
                        title : '检定有限期',
                },
            {
                    fixed : 'right',
                    title : '操作',
                    align : 'center',
                    toolbar : '#toobar',
                    width:200
                 }
        ] ]

        });
            table.on('checkbox(coreDeviceTable)', function(obj){
                var coreDevice = obj.data;
                if(obj.checked){
                    //按钮逻辑Lib.buttonEnable()
                }else{

                }
            })
            //监听工具条
            table.on('tool(coreDeviceTable)', function(obj) {
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值
                if(layEvent === 'update'){
                    var url = "/cms/coreDevice/edit.do?id="+data.id;
                    Common.openDlg(url,"检测设备管理>"+data.id+">编辑");
                }else if(layEvent === 'del'){
                    layui.use(['del'], function(){
                        var delView = layui.del;
                        delView.delRow(data);
                    });
                }
            });
        },
        initSearchForm:function(){
            Lib.initSearchForm( $("#searchForm"),coreDeviceTable,form);
        },
        initToolBar:function(){
            toolbar = {
                add : function() { // 获取选中数据
                    var url = "/cms/coreDevice/add.do";
                    Common.openDlg(url,"检测设备管理>新增");
                },
                batchDel : function() {
                    layui.use(['del'], function(){
                        var delView = layui.del
                        delView.delBatch();
                    });
                }
                ,exportDocument : function() {
                    layui.use([ 'coreDeviceApi' ], function() {
                        var coreDeviceApi = layui.coreDeviceApi
                        Common.openConfirm("确认要导出这些检测设备数据?", function() {
                            coreDeviceApi.exportExcel($("#searchForm"), function(fileId) {
                                Lib.download(fileId);
                            })
                        })
                    });
                },
                importDocument:function(){
                    var uploadUrl = Common.ctxPath+"/cms/coreDevice/excel/import.do";
                    //模板,
                    var templatePath= "/cms/coreDevice/coreDevice_upload_template.xls";
                    //公共的简单上传文件处理
                    var url = "/core/file/simpleUpload.do?uploadUrl="+uploadUrl+"&templatePath="+templatePath;
                    Common.openDlg(url, "检测设备管理>上传");
                }
               };

            //监听工具条
            layui.table.on('tool(coreDeviceTable)', function(obj) {
                var data = obj.data //获得当前行数据
                    , layEvent = obj.event; //获得 lay-event 对应的值
                if(layEvent === 'update'){
                    var url = "/cms/coreDevice/edit.do?id="+data.id;
                    Common.openDlg(url,"编辑");
                }else if(layEvent=='del') {
                    layui.use(['del'], function(){
                        var delView = layui.del;
                        delView.delRow(data);
                    });
                }
            });
            $('.ext-toolbar').on('click', function() {
        var type = $(this).data('type');
        toolbar[type] ? toolbar[type].call(this) : '';
    });
        }
     };
    exports('index',view);

});